﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using datatablegenerator.Entity;
using datatablegenerator.Common;

namespace datatablegenerator.Common
{
    public class DataTypeHelper
    {
        //数据类型
        private static readonly List<string> TypeList;

        private static readonly Dictionary<string, string> Dic;
        static DataTypeHelper()
        {
            TypeList = new List<string> { "char", "varchar", "varchar(max)", "nchar", "nvarchar", "nvarchar(max)", "bit", "binary", "varbinary", "varbinary(max)", "image", "int", "bigint", "decimal", "float", "datetime", "datetime2", "date", "time" };

            Dic = new Dictionary<string, string>();
            Dic.Add("PK", "primary key");
            Dic.Add("UK", "unique");
        }

        /// <summary>
        /// 数据类型
        /// </summary>
        /// <returns></returns>
        public static List<string> ListDataType()
        {
            return TypeList;
        }


        /// <summary>
        /// 判断是否是字符类型
        /// </summary>
        /// <param name="charStr">数据类型</param>
        /// <returns>TRUE: 是字符类型；FALSE:其他类型</returns>
        public static bool IsCharType(string charStr)
        {
            string tempStr = charStr.Split('(').ToList().Take(1).First();

            switch (tempStr)
            {
                case "char":
                    return true;
                case "varchar":
                    return true;
                case "nchar":
                    return true;
                case "nvarchar":
                    return true;
                default:
                    return false;
            }
        }

        /// <summary>
        /// 字段约束状态
        /// </summary>
        /// <param name="statusStr"></param>
        /// <returns></returns>
        public static FunctionResult ConstraintStatus(string statusStr, List<DatatableDTO> dto)
        {
            FunctionResult result = new FunctionResult();

            string[] strArr = statusStr.Split(',');

            if (strArr.Length != 1)
            {
                string[] fieldNameArr = new string[strArr.Length];  //字段名称数组
                for (int i = 0; i < strArr.Length; i++)
                {
                    if (strArr[i].ToUpper() == "PK")
                    {
                        result.Success = false;
                        result.Content = $"请确定主键约束是否正确！";
                        return result;
                    }

                    var fieldInfo = dto.Where(r => r.Constraint == strArr[i]).FirstOrDefault();
                    if (fieldInfo == null)
                    {
                        result.Success = false;
                        result.Content = $"{strArr[i]},约束不存在！";
                        Array.Clear(fieldNameArr, 0, strArr.Length);
                        return result;
                    }
                    fieldNameArr[i] = fieldInfo.Name; //字段名称
                }

                result.Success = true;
                result.Content = $"unique({string.Join(',', fieldNameArr)})";
                return result;
            }

            if (string.IsNullOrWhiteSpace(statusStr))
            {
                result.Success = true;
                return result;
            }
            else
            {
                result.Success = true;
                result.Content = Dic[statusStr.Substring(0, 2).ToUpper()];
                return result;
            }
        }
    }
}
